//
//  ECMineCourseTableViewCell.m
//  Edu_iOS2018
//
//  Created by apple on 2018/1/11.
//  Copyright © 2018年 yanyan. All rights reserved.
//


#import "ECMineCourseTableViewCell.h"
#import "ECMineCourseLearnModel.h"
#import "ContactDateHelper.h"

@interface ECMineCourseTableViewCell(){
    ///进度数字显示lab
    UILabel *progressLab;
    ///进图条
    UIProgressView *progressView;
    ///学习时长lab
    UILabel *learnTimeLab;
    ///课程图片
    UIImageView *imageview;
    ///课程标题
    UILabel *titleLab;
}
@end


@implementation ECMineCourseTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = 0;
        [self setCellUI];
    }
    return self;
}

-(void)setCellUI{
    ///图片Image
    imageview = [[UIImageView alloc]init];
    [self.contentView addSubview:imageview];
    imageview.layer.masksToBounds = YES;
    imageview.layer.cornerRadius = 4.f;
    ///课程标题lab
    titleLab = [[UILabel alloc]init];
    [self.contentView addSubview:titleLab];
    titleLab.numberOfLines = 2;
    titleLab.font = [UIFont systemFontOfSize:13*kPercentage];

    progressView = [[UIProgressView alloc]init];
    [self.contentView addSubview:progressView];
    progressView.trackTintColor = [UIColor colorWithHexString:@"#d7d7d7"];
    progressView.progressTintColor = [UIColor colorWithHexString:@"ff8400"];//应该有渐变颜色的 暂时搁置
    
    progressLab = [[UILabel alloc]init];
    [self.contentView addSubview:progressLab];
    progressLab.textColor = [UIColor colorWithHexString:@"ff6600"];
    progressLab.textAlignment = NSTextAlignmentRight;
   
    learnTimeLab = [[UILabel alloc]init];
    [self.contentView addSubview:learnTimeLab];
    learnTimeLab.textColor = [UIColor colorWithHexString:@"#808080"];
    learnTimeLab.font = [UIFont systemFontOfSize:13*kPercentage];
    
    UIView *bottonView = [[UIView alloc]init];
    [self.contentView addSubview:bottonView];
    bottonView.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
    
    WeakSelf(weakSelf);
    [imageview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView.mas_top).offset(kProportion_Height(48));
        make.left.equalTo(weakSelf.contentView.mas_left).offset(kProportion_Width(45));
        make.width.equalTo(@(kProportion_Width(324)));
//        make.height.equalTo(imageview.mas_width).multipliedBy(162.f/324.f);
        make.bottom.equalTo(weakSelf.contentView.mas_bottom).offset(-kProportion_Height(48));
    }];
    
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageview.mas_top);
        make.left.equalTo(imageview.mas_right).offset(kProportion_Width(36));
        make.right.equalTo(weakSelf.contentView.mas_right).offset(-kProportion_Width(45));
        make.bottom.equalTo(progressView.mas_top).offset(-kProportion_Width(50));
    }];
    
    [progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLab.mas_left);
        make.bottom.equalTo(imageview.mas_bottom).offset(2);
        make.width.equalTo(@(kProportion_Width(450)));
        make.height.equalTo(progressView.mas_width).multipliedBy(9.f/480.f);
    }];
    
    [progressLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(progressView.mas_right).offset(kProportion_Width(50));
        make.bottom.equalTo(progressView.mas_bottom).offset(2);
        make.right.equalTo(weakSelf.contentView.mas_right).offset(-15);
        make.height.equalTo(@17);
    }];
    
    [bottonView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageview.mas_bottom).offset(kProportion_Height(48));
        make.width.equalTo(weakSelf.contentView.mas_width);
        make.height.equalTo(@(kProportion_Height(24)));
    }];
    
    [learnTimeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLab.mas_left);
        make.right.equalTo(progressLab.mas_left);
        make.bottom.equalTo(imageview.mas_bottom);
        make.height.equalTo(@14);
    }];
    
}

-(void)hiddenProgressViewWithTag:(NSInteger)tag{
    if (tag == 10) {
        [progressView setHidden:NO];
        [progressLab setHidden:NO];
        
        [learnTimeLab setHidden:YES];
    }else{
        [progressView setHidden:YES];
        [progressLab setHidden:YES];
        
        [learnTimeLab setHidden:NO];
    }
}

-(void)setContentMode:(ECMineCourseLearnModel *)contentMode{
    [imageview sd_setImageWithURL:[NSURL URLWithString:contentMode.middlePicture] placeholderImage:[UIImage imageNamed:kEduCourseImagePlaceholderName]];
    titleLab.text = contentMode.title;
    progressLab.text = [NSString stringWithFormat:@"%ld%%",contentMode.learnedPercent];
    float pre = contentMode.learnedPercent/100.f;
    progressView.progress = pre;
    learnTimeLab.text = [NSString stringWithFormat:@"学习时长: %@",[[ContactDateHelper sharedInstance] getMMSSFromSS:[NSString stringWithFormat:@"%ld",contentMode.learnedMinutes]]];
}



@end
